// 抽奖轮盘列表页面
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    wheels: [], // 抽奖轮盘列表
    showDeleteModal: false, // 是否显示删除确认框
    currentWheel: null, // 当前选中的轮盘
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.loadWheels();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 每次页面显示时刷新数据
    this.loadWheels();
  },

  /**
   * 加载轮盘列表
   */
  loadWheels: function () {
    this.setData({ loading: true });
    
    // 从本地存储获取轮盘数据
    const wheels = wx.getStorageSync('lucky_wheels') || [];
    
    // 延迟一点时间显示，提供更好的用户体验
    setTimeout(() => {
      this.setData({
        wheels: wheels,
        loading: false
      });
    }, 500);
  },

  /**
   * 跳转到创建轮盘页面
   */
  onCreateWheel: function () {
    wx.navigateTo({
      url: '/pages/lucky-wheel/config/index'
    });
  },

  /**
   * 跳转到编辑轮盘页面
   */
  onEditWheel: function (e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/lucky-wheel/config/index?id=${id}`
    });
  },

  /**
   * 跳转到抽奖页面
   */
  onStartDraw: function (e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/lucky-wheel/draw/index?id=${id}`
    });
  },

  /**
   * 显示删除确认框
   */
  onShowDeleteModal: function (e) {
    const id = e.currentTarget.dataset.id;
    const wheels = this.data.wheels;
    const currentWheel = wheels.find(item => item.id === id);
    
    this.setData({
      showDeleteModal: true,
      currentWheel
    });
  },

  /**
   * 关闭删除确认框
   */
  onCancelDelete: function () {
    this.setData({
      showDeleteModal: false,
      currentWheel: null
    });
  },

  /**
   * 确认删除轮盘
   */
  onConfirmDelete: function () {
    const wheels = this.data.wheels;
    const currentId = this.data.currentWheel.id;
    
    // 过滤掉要删除的轮盘
    const updatedWheels = wheels.filter(item => item.id !== currentId);
    
    // 更新存储和页面数据
    wx.setStorageSync('lucky_wheels', updatedWheels);
    
    this.setData({
      wheels: updatedWheels,
      showDeleteModal: false,
      currentWheel: null
    });
    
    wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 2000
    });
  },

  /**
   * 格式化时间
   */
  formatDate: function (timestamp) {
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');
    
    return `${year}-${month}-${day} ${hour}:${minute}`;
  },

  /**
   * 生成轮盘预览的彩色区域
   */
  generatePreviewStyle: function (items) {
    if (!items || items.length === 0) return '';
    
    let style = '';
    const totalItems = items.length;
    const anglePerItem = 360 / totalItems;
    
    items.forEach((item, index) => {
      const startAngle = index * anglePerItem;
      const endAngle = (index + 1) * anglePerItem;
      style += `conic-gradient(${item.color} ${startAngle}deg, ${item.color} ${endAngle}deg), `;
    });
    
    // 移除最后的逗号和空格
    return style.slice(0, -2);
  }
}) 